package com.sky.myapplication.java.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sky.myapplication.R;

public class MyItemView extends RelativeLayout {
    private ImageView iv_left, iv_right;
    private TextView tv_center;

    public MyItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MyItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs){
        inflate(context, R.layout.view_my_item, this);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ItemView);

        iv_left = findViewById(R.id.iv_left);
        iv_right = findViewById(R.id.iv_right);
        tv_center = findViewById(R.id.tv_center);

        int leftIcon = ta.getResourceId(R.styleable.ItemView_ivLeft, R.drawable.ic_launcher_background);
        iv_left.setImageResource(leftIcon);

        int rightIcon = ta.getResourceId(R.styleable.ItemView_ivRight, R.drawable.my_qkk_jt);
        iv_right.setImageResource(rightIcon);

        int color = ta.getColor(R.styleable.ItemView_tvCenterColor, Color.BLUE);
        tv_center.setTextColor(color);

        String text = ta.getString(R.styleable.ItemView_tvCenterText);
        if (text != null){
            tv_center.setText(text);
        }
        ta.recycle();
    }

    public CharSequence getText(){
        return tv_center.getText();
    }

    public void setText(String text){
        tv_center.setText(text);
    }
}
